package org.idea.qiyu.red.packet.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.idea.qiyu.pay.service.IPayService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;
import static org.idea.qiyu.red.packet.config.InitRedPacketConfig.packetMoneyArr;
import static org.idea.qiyu.red.packet.service.InitBatchSendRedPacketService.blockingQueue;

/**
 * @Author linhao
 * @Date created in 9:02 下午 2021/11/13
 */
@RestController
@RequestMapping(value = "/red-packet")
public class RedPacketController {

    public static AtomicInteger packetMoneyIndex = new AtomicInteger(0);

    public static AtomicInteger sendCount = new AtomicInteger(0);

//    @DubboReference(async = true)
//    private IPayService iPayService;

    @GetMapping(value = "/get-packet-money")
    public int getPacketMoney(@RequestParam(defaultValue = "-1") long userId) throws InterruptedException {
        int index = packetMoneyIndex.getAndIncrement();
        if (index > 50 * 10000 -1){
            return 0;
        }else {
            sendCount.incrementAndGet();
            short money = packetMoneyArr[index];
            blockingQueue.offer(money + ";"+userId);
//            iPayService.sendMoneyToUser(userId, (int) money);
            return money;
        }
    }
}
